package com.sopra.tecnica.poc.server.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.sopra.tecnica.poc.server.entity.OrderEntity;
import com.sopra.tecnica.poc.server.service.CustomerService;
import com.sopra.tecnica.poc.server.service.OrderService;
import com.sopra.tecnica.poc.server.util.SaveFileJasperReportsPdfView;
import com.sopra.tecnica.poc.server.util.Signature;

@Controller
@RequestMapping("order")
public class OrderController /*implements ServletContextAware*/ {
	
//	private ServletContext servletContext;
//	private ServletContextResource servletContextResource;
	
	@Autowired
	private SaveFileJasperReportsPdfView view;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private CustomerService customerService;

	@RequestMapping("customer")
	public void findLastYearOrders(@RequestParam("code") String code,
			Model model) {
		List<OrderEntity> result = orderService.findLastYearCustomerOrders(code);
		model.addAttribute(result);
	}
	
	@RequestMapping("report")
	public String reportOrder(
			@RequestParam("orderNum") int orderNum,
			Model model) throws IOException {

		File signatureFile = File.createTempFile("sig", ".tmp");
		model.addAttribute("signatureFile", Signature.createFile(signatureFile, ""));
		
		model.addAttribute("customerData", customerService.findOrderCustomer(orderNum));
		model.addAttribute("itemData", orderService.itemsForReport(orderNum));
		
		// appel de la bonne view
		return "orderReport";
	}
	
	@RequestMapping(value = "report", params = "path")
	public ModelAndView reportOrder(
			@RequestParam("orderNum") int orderNum,
			@RequestParam("path") String path,
			ModelMap model) throws IOException {
		
		File signatureFile = File.createTempFile("sig", ".tmp");
		model.addAttribute("signatureFile", Signature.createFile(signatureFile, path));
		
		model.addAttribute("customerData", customerService.findOrderCustomer(orderNum));
		model.addAttribute("itemData", orderService.itemsForReport(orderNum));
		
		File pdfFile = File.createTempFile("report", ".pdf");
		System.out.println(pdfFile.getAbsolutePath());
		
		model.addAttribute("outputFile", pdfFile);
		//view.addExporterParameter(JRExporterParameter.OUTPUT_FILE, pdfFile);
		
		model.addAttribute("mail", "martin.soubeyrand@gmail.com");
		
		return new ModelAndView(view, model);
	}
	
//	@Override
//	public void setServletContext(ServletContext servletContext) {
//		this.servletContext = servletContext;
//		this.servletContextResource = new ServletContextResource(servletContext, "/");
//	}
}
